
#include  "CS.h"

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

LockBase::LockBase()
{

}

LockBase::~LockBase()
{

}

SafeLock::SafeLock()
{
    m_pLock = NULL;
}
SafeLock::SafeLock(LockBase* pLock)
{
    m_pLock = pLock;
    if (m_pLock)
    {
        m_pLock->Lock();
    }
}
SafeLock::~SafeLock()
{
    if (m_pLock)
    {
        m_pLock->UnLock();
        m_pLock = NULL;
    }
}

#ifdef  _WIN32

DerivedLock::DerivedLock()
{
    InitializeCriticalSection(&m_cs);
}

DerivedLock::~DerivedLock()
{
    DeleteCriticalSection(&m_cs);
}

void DerivedLock::Lock()
{
    ::EnterCriticalSection(&m_cs);
}

void DerivedLock::UnLock()
{
    ::LeaveCriticalSection(&m_cs);
}

void DerivedLock::Destroy()
{
    DeleteCriticalSection(&m_cs);
}

#else

DerivedLock::DerivedLock()
{
    pthread_mutex_init(&m_cs, NULL);
}

DerivedLock::~DerivedLock()
{
    pthread_mutex_destroy(&m_cs);
}

void DerivedLock::Lock()
{
    pthread_mutex_lock(&m_cs);
}

void DerivedLock::UnLock()
{
    pthread_mutex_unlock(&m_cs);
}

void DerivedLock::Destroy()
{
    pthread_mutex_destroy(&m_cs);
}

#endif




